﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace drawing_print.util.file
{
    public class Mosaic
    {
        /// <summary>
        /// 马赛克
        /// </summary>
        /// <param name="img"></param>
        /// <param name="a"></param>
        /// <returns></returns>
        public static Bitmap imgMosaic(Bitmap img, int a)  //img为输入的图像，a为马赛克的范围
        {
            for (int h = 0; h < img.Height; h += a)         //两层循环，遍历每一个间隔的像素点
            {
                for (int w = 0; w < img.Width; w += a)
                {
                    Color xxx = img.GetPixel(w, h);         //获取颜色
                    for (int x = w; (x < w + a && x < img.Width); x++)        //另一层循环，填充颜色
                    {
                        for (int y = h; (y < h + a && y < img.Height); y++)
                        {
                            img.SetPixel(x, y, xxx);
                        }
                    }
                }
            }
            return img;
        }
        /// <summary>
        /// 截取
        /// </summary>
        /// <param name="src"></param>
        /// <param name="cropRect"></param>
        /// <returns></returns>
        public static Bitmap crop(Bitmap src,Rectangle cropRect)
        {
            Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
                      cropRect,
                      GraphicsUnit.Pixel);
            }
            return target;
        }

        /// <summary>
        /// 马赛克处理
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="effectWidth"> 影响范围 每一个格子数 </param>
        /// <returns></returns>
        public static Bitmap AdjustTobMosaic(System.Drawing.Bitmap bitmap, int effectWidth)
        {
            // 差异最多的就是以照一定范围取样 玩之后直接去下一个范围
            for (int heightOfffset = 0; heightOfffset < bitmap.Height; heightOfffset += effectWidth)
            {
                for (int widthOffset = 0; widthOffset < bitmap.Width; widthOffset += effectWidth)
                {
                    int avgR = 0, avgG = 0, avgB = 0;
                    int blurPixelCount = 0;

                    for (int x = widthOffset; (x < widthOffset + effectWidth && x < bitmap.Width); x++)
                    {
                        for (int y = heightOfffset; (y < heightOfffset + effectWidth && y < bitmap.Height); y++)
                        {
                            System.Drawing.Color pixel = bitmap.GetPixel(x, y);

                            avgR += pixel.R;
                            avgG += pixel.G;
                            avgB += pixel.B;

                            blurPixelCount++;
                        }
                    }

                    // 计算范围平均
                    avgR = avgR / blurPixelCount;
                    avgG = avgG / blurPixelCount;
                    avgB = avgB / blurPixelCount;


                    // 所有范围内都设定此值
                    for (int x = widthOffset; (x < widthOffset + effectWidth && x < bitmap.Width); x++)
                    {
                        for (int y = heightOfffset; (y < heightOfffset + effectWidth && y < bitmap.Height); y++)
                        {

                            System.Drawing.Color newColor = System.Drawing.Color.FromArgb(avgR, avgG, avgB);
                            bitmap.SetPixel(x, y, newColor);
                        }
                    }
                }
            }
            return bitmap;
        }
    }
}
